Understanding Thread/BeginInvoke? [beginner]
Posted
by Moberg
on Stack Overflow
See other posts from Stack Overflow
or by Moberg
Published on 2010-04-22T14:49:07Z
Indexed on
2010/04/22
15:03 UTC
Read the original article
Hit count: 305
Consider the code:
class Work
{
public void DoStuff(string s)
{
Console.WriteLine(s);
// .. whatever
}
}
class Master
{
private readonly Work work = new Work();
public void Execute()
{
string hello = "hello";
// (1) is this an ugly hack ?
var thread1 = new Thread(new ParameterizedThreadStart(o => this.work.DoStuff((string)o)));
thread1.Start(hello);
thread1.Join();
// (2) is this similar to the one above?
new Action<string>(s => this.work.DoStuff(s)).BeginInvoke(hello, null, null);
}
}
Is (1) an acceptable way of easy starting some work in a seperate thread? If not a better alternative would be much appreciated.
Is (2) doing the same? I guess what I ask is if a new thread is started, or..
Hope you can help a beginner to a better understanding :)
/Moberg
© Stack Overflow or respective owner